import { eleEndConfigs, EleEndState } from "@/elevatorCat/endState/state";
import { elevatorCatModule } from "@/elevatorCat/module";
import { DisplaySlotId, world } from "@minecraft/server";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { ScoreBoardDataBase } from "sapi-pro";
import { ElevatorBellComponent } from "./components/bell";
import { ElevatorCatchComponent } from "./components/catCatch";
import { EleCatchStateDaemon } from "./components/daemon";
import { ElevatorGameEndComponent } from "./components/gameEnd";
import { ElevatorLocatorComponent } from "./components/locator";
import { ElevatorSheepComponent } from "./components/sheep";

export class eleCatchState extends elevatorCatModule.State {
    readonly showDb = new ScoreBoardDataBase(
        "elevator_show",
        "§6§l电梯猫",
        false
    );
    /**是否放羊 */
    isSheep: boolean = false;

    override onEnter(): void {
        this.addComponent(Timer, {
            initialTime: this.context.config.time,
            autoStart: true,
            compensate: true,
        })
            .addComponent(ElevatorGameEndComponent)
            .addComponent(EleCatchStateDaemon)
            .addComponent(ElevatorCatchComponent)
            .addComponent(ElevatorLocatorComponent);

        if (this.context.config.bellMode) {
            this.addComponent(ElevatorBellComponent);
            const timer = this.getComponent(Timer);
            this.subscribe(
                timer.events.onTime,
                () => {
                    this.addComponent(ElevatorSheepComponent);
                },
                { time: 60 }
            );
        }
        world.gameRules.pvp = true;

        this.showDb.setDisplaySlot(DisplaySlotId.Sidebar);
    }

    override onExit(): void {
        world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar);
    }

    toEnd(config: eleEndConfigs) {
        this.transitionTo(EleEndState, config);
    }
}
